<template>
  <div class="app-container">
    <searchForm  :formOptions="formOptions"
      @onSearch="onSearch"
      @onReset="onReset"
      :formLabelWidth="formLabelWidth"
      v-show="showSearch"></searchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['clr:workLog:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['clr:workLog:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['clr:workLog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['clr:workLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="辅导员姓名" align="center" prop="clrName">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.teacherId"/>
        </template>-->
      </el-table-column>
      <el-table-column label="教工号" align="center" prop="tno">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.userId"/>
        </template>-->
      </el-table-column>
      <el-table-column label="日志频率类型" align="center" prop="rate">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cps_rate_type" :value="scope.row.rate" />
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.title"/>
        </template>-->
      </el-table-column>
      <el-table-column label="工作日志类型" align="center" prop="titleType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.clr_work_log_type" :value="scope.row.titleType" />
        </template>
      </el-table-column>
      <el-table-column label="填报日期" align="center" prop="fillingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fillingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="附件" align="center" prop="annex" width="180">
        <template slot-scope="scope">
          
            <el-button size="mini" type="text" @click="handleExportAnnex(scope.row)">{{  scope.row.annex ? '下载文件': "暂无文件"}}</el-button>
          
        </template>
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fillingDate, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.remark"/>
        </template>-->
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['clr:workLog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['clr:workLog:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改辅导员工作日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工作日志类型" prop="titleType">
              <el-select v-model="form.titleType" placeholder="请选择工作日志类型"  style="width: 100%;">
                <el-option
                  v-for="dict in dict.type.clr_work_log_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="日志频率类型" prop="rate">
              <el-select v-model="form.rate" placeholder="请选择日志频率类型"  style="width: 100%;">
                <el-option
                  v-for="dict in dict.type.cps_rate_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" maxlength="30"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="content">
              <editor v-model="form.content" :min-height="192" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="附件" prop="annex">
              <fileUpload @input="uoload"></fileUpload>
              <!-- <el-input v-model="form.annex" placeholder="请输入附件" /> -->
            </el-form-item>
          </el-col>
         
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" maxlength="100"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWorkLog,
  getWorkLog,
  delWorkLog,
  addWorkLog,
  updateWorkLog
} from "@/api/clr/workLog";
import { getDicts } from "@/api/system/dict/type";
import searchForm from "@/components/Search/searchForm.vue";
import fileUpload from "@/views/clr/achievement/components/uploud.vue";
export default {
  name: "WorkLog",
  components: {fileUpload,searchForm},
  dicts: ["clr_work_log_type","cps_rate_type"],
  data() {
    return {
      formLabelWidth: "100px",
      formOptions: [
        {
          label: "辅导员姓名",
          prop: "clrName",
          element: "el-input",
          placeholder: "请输入辅导员姓名"
        },
        {
          label: "教工号",
          prop: "tno",
          element: "el-input",
          placeholder: "请输入教工号"
        },
        {
          label: "标题",
          prop: "title",
          element: "el-input",
          placeholder: "请输入标题"
        },
        {
          label: "日志频率类型",
          prop: "rate",
          element: "el-select",
          options: [],
          placeholder: "请选择日志频率类型"
        },
        {
          label: "工作日志类型",
          prop: "titleType",
          element: "el-select",
          options: [],
          placeholder: "请选择工作日志类型"
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 辅导员工作日志表格数据
      workLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherId: null,
        userId: null,
        rate: null,
        title: null,
        titleType: null,
        content: null,
        annex: null,
      },
      // 表单参数
      form: {},
      clrNames: [],
      // 表单校验
      rules: {
        titleType: [
          { required: true, message: "请选择工作日志类型", trigger: "change" }
        ],
        rate: [
          { required: true, message: "请选择日志频率类型", trigger: "change" }
        ],
        title: [
          { required: true, message: "请输入标题",trigger: "bulr" }
        ],
        content: [
          { required: true, message: "请输入内容",trigger: "bulr" }
        ]

      }
    };
  },
  computed:{
    getRouteParams () {
      return this.$route.params
    }
  },
  watch:{
    getRouteParams:{
      handler(val){
        if (val.rate && val.titleType) {
          this.handleAdd(val)
        }
      },
      immediate: true
    },
  },
  created() {
    this.getList();
    this.getParams()
  },
  methods: {
   async getParams() {
      let res = await getDicts("clr_work_log_type")
      if(res.code == 200) {
        this.formOptions[4].options = res.data.map(item => {
        return {
          value: item["dictValue"],
          label: item["dictLabel"]
        };
      });
      }
      let restype = await getDicts("cps_rate_type")
      if(restype.code == 200) {
        this.formOptions[3].options = restype.data.map(item => {
        return {
          value: item["dictValue"],
          label: item["dictLabel"]
        };
      });
      }
    },
    uoload(data) {
      console.log(data,'上传')
      this.form.annex = data
    },
     // 获取搜索表单提交的数据
     onSearch(val) {
      // 查询条件和分页数据合并
      this.queryParams = Object.assign(this.queryParams, val);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    onReset(data) {
      console.log(data);
      this.queryParams.pageNum = 1;
      this.queryParams = Object.assign(this.queryParams, data);
      console.log(this.queryParams);
      this.getList();
    },
    /** 查询辅导员工作日志列表 */
    getList() {
      this.loading = true;
      listWorkLog(this.queryParams).then(response => {
        this.workLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cwlId: null,
        teacherId: null,
        userId: null,
        rate: null,
        title: null,
        titleType: null,
        content: null,
        annex: null,
        fillingDate: null,
        remark: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cwlId);
      this.clrNames = selection.map(item => item.clrName);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(obj) {
      this.reset();
      if (obj) {
        this.form.titleType = obj.titleType
        this.form.rate = obj.rate
      }
      this.open = true;
      this.title = "添加辅导员工作日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cwlId = row.cwlId || this.ids;
      getWorkLog(cwlId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改辅导员工作日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cwlId != null) {
            updateWorkLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWorkLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cwlIds = row.cwlId || this.ids;
      this.clrNames = row.clrName || this.clrNames;
      this.$modal
        .confirm('是否确认删除辅导员"' + this.clrNames + '"工作日志的数据项？')
        .then(function() {
          return delWorkLog(cwlIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
         // 下载附件
         handleExportAnnex(row) {
          console.log(row,'下载')
      let link = document.createElement("a");
      //创建一个a标签
      link.style.display = "none";
      //将a标签隐藏
      link.href = process.env.VUE_APP_BASE_API + row.annex;
      //给a标签添加下载链接  "域名+接口"  safe是一个动态的域名  后面的接口替换成你自己的下载接口
      link.setAttribute("download",row.createBy + '工作日志');
      // 此处注意，要给a标签添加一个download属性，属性值就是文件名称 否则下载出来的文件是没有属性的，空白白
      document.body.appendChild(link);
      //将上面创建的a标签加入到body的尾部
      link.click();
      // 执行a标签
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/clr/workLog/export",
        {
          ...this.queryParams
        },
        `workLog_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
